
package org.winexpress

import org.codehaus.groovy.grails.web.json.JSONObject;
import org.codehaus.groovy.grails.web.json.JSONArray;
import grails.converters.JSON

/**
 *
 * @author Swee Zhi
 */
class CustomerManagement {
	
    def listCustomers() {
        def Customers = Customer.list()
        ShoppingCartManagement scman = new ShoppingCartManagement()        
        JSONArray arr = new JSONArray()
        Customers.each() {
            JSONObject obj = new JSONObject()
            obj.put("id",it.id)
            obj.put("username",it.username)
            obj.put("password",it.password)
            obj.put("lastLogin",it.lastLogin)
            obj.put("activationStatus",it.activationStatus)
            obj.put("lastName",it.lastName)
            obj.put("firstName",it.firstName)
            obj.put("birthdate",it.birthdate)
            obj.put("email",it.email)
            obj.put("blkHouseNo",it.blkHouseNo)
            obj.put("streetName",it.streetName)
            obj.put("streetName",it.streetName)
            obj.put("postalCode",it.postalCode)
            obj.put("telNo",it.telNo)
            obj.put("countryId",it.country.id)
//            def cart = scman.getShoppingCartByCustomerID(it.id)
//            ArrayList cartId = cart.id
//            obj.put("cartId",cartId.get(0))
//            ArrayList uniqueK = cart.id
//            obj.put("uniqueKey",uniqueK.get(0))
        }
        
        return arr
    }
    
    def getCustomerByID(id) {
        def customer = Customer.findById(id)
        if (customer == null) {
            return null
        }
        else {
            ShoppingCartManagement scman = new ShoppingCartManagement()        
            JSONObject obj = new JSONObject()
            obj.put("id",customer.id)
            obj.put("username",customer.username)
            obj.put("password",customer.password)
            obj.put("lastLogin",customer.lastLogin)
            obj.put("activationStatus",customer.activationStatus)
            obj.put("lastName",customer.lastName)
            obj.put("firstName",customer.firstName)
            obj.put("birthdate",customer.birthdate)
            obj.put("email",customer.email)
            obj.put("blkHouseNo",customer.blkHouseNo)
            obj.put("streetName",customer.streetName)
            obj.put("unitNo",customer.unitNo)
            obj.put("postalCode",customer.postalCode)
            obj.put("telNo",customer.telNo)
            obj.put("countryId",customer.country.id)
            def carts = customer.shoppingCarts.toList()
            def cart = carts[0]
            obj.put("shoppingCartId", cart.id.toInteger())
            obj.put("uniqueKey", cart.uniqueKey)
//            def cart = scman.getShoppingCartByCustomerID(customer.id)
//            ArrayList cartId = cart.id
//            obj.put("cartId",cartId.get(0))
//            ArrayList uniqueK = cart.id
//            obj.put("uniqueKey",uniqueK.get(0))

            return obj
        }
    }
    
    def getCustomerByUsername(username) {
        def customer = Customer.findByUsername(username)
        if (customer == null) {
            return null
        }
        else {
            ShoppingCartManagement scman = new ShoppingCartManagement()        
            JSONObject obj = new JSONObject()
            obj.put("id",customer.id)
            obj.put("username",customer.username)
            obj.put("password",customer.password)
            obj.put("lastLogin",customer.lastLogin)
            obj.put("activationStatus",customer.activationStatus)
            obj.put("lastName",customer.lastName)
            obj.put("firstName",customer.firstName)
            obj.put("birthdate",customer.birthdate)
            obj.put("email",customer.email)
            obj.put("blkHouseNo",customer.blkHouseNo)
            obj.put("streetName",customer.streetName)
            obj.put("unitNo",customer.unitNo)
            obj.put("postalCode",customer.postalCode)
            obj.put("telNo",customer.telNo)
            obj.put("countryId",customer.country.id)
            def carts = customer.shoppingCarts.toList()
            def cart = carts[0]
            obj.put("shoppingCartId", cart.id.toInteger())
            obj.put("uniqueKey", cart.uniqueKey)
//            def cart = scman.getShoppingCartByCustomerID(customer.id)
//            ArrayList cartId = cart.id
//            obj.put("cartId",cartId.get(0))
//            ArrayList uniqueK = cart.id
//            obj.put("uniqueKey",uniqueK.get(0))
            return obj
        }
    }
    
    def getCustomerForDbByCustomerID(id) {
        def c = Customer.findById(id)
        
        return c
    }
    
    def addCustomer(c) {
        def status
        
        if (c.save(flush:true)) {
            status = true
        }
        else {
            status = false
        }        
        println 'Management >> Add customer >> ' + status
        
        return status
    }
    
    def updateCustomer(customer) {
        def status
        
        Customer c = Customer.findById(customer.id)
        if (customer.has("password")) { c.password = customer.get("password"); println c.password }
//        if (customer.has("activationStatus")) { c.activationStatus = customer.get("activationStatus"); println c.activationStatus }
        if (customer.has("email")) { c.email = customer.get("email"); println c.email }
        if (customer.has("blkHouseNo")) { c.blkHouseNo = customer.get("blkHouseNo"); println c.blkHouseNo }
        if (customer.has("streetName")) { c.streetName = customer.get("streetName"); println c.streetName }
        if (customer.has("unitNo")) { c.unitNo = customer.get("unitNo"); println c.unitNo }
        if (customer.has("country")) { c.country = customer.get("country"); println c.countryId }
        if (customer.has("postalCode")) { c.postalCode = customer.get("postalCode"); println c.postalCode }
        if (customer.has("telNo")) { c.telNo = customer.get("telNo"); println c.telNo  }
//        Date d = new Date();
//        c.lastLogin = d
        
        if (c.save(flush:true)) {
            status = true
        }
        else {
            status = false
        }        
        println 'Management >> Update customer >> ' + status
        
        return status
    }
    
    def updateLastLogin(customer) {
        def status
        
        Customer c = Customer.findById(customer.id)
        Date d = new Date();
        c.lastLogin = d
        
        if (c.save(flush:true)) {
            status = true
        }
        else {
            status = false
        }
        return status
    }
    
    def updatePassword(customer) {
        def status
        
        Customer c = Customer.findById(customer.id)
        
        if (c.save(flush:true)) {
            status = true
        }
        else {
            status = false
        }
        return status
    }
    
    def updateActivationStatus(customer) {
        def status
        
        Customer c = Customer.findById(customer.id)
        if (customer.has("activationStatus")) { c.activationStatus = customer.get("activationStatus"); println c.activationStatus }
        
        if (c.save(flush:true)) {
            status = true
        }
        else {
            status = false
        }
        return status
    }
    
    def deleteCustomer(id) {
        def status
        
        Customer c = Customer.findById(id)
        if (c.delete()) {
            status = true
        }
        else {
            status = false
        }        
        println 'Management >> Delete customer >> ' + status
        
        return status
    }
}

